<?php


namespace Frieza\Modules\Position\Actions;

use Doraemon\model\AdmOperateLog;
use Doraemon\model\Phone;
use Doraemon\service\Identifyingcode;
use Doraemon\service\Member\PhoneCrypto;
use Doraemon\tools\Checker;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ClearPhoneCache extends RowAction
{

    public $name = '清除短信验证码限制';

    public function handle($uid,Request $request)
    {
        $new_phone = $request->get("new_phone");
        $admin_id = Admin::user()->id;
        $admin_name = Admin::user()->username;
        if (empty($new_phone)) {
            return $this->response()->error('请输入手机号');
        }
        if (!is_numeric($new_phone)) {
            return $this->response()->error('手机号输入有误');
        }
        if (!preg_match("/^[0-9]+$/", $new_phone)) {
            return $this->response()->error('手机号格式错误');
        }
//        $decrypt_phone = PhoneCrypto::decrypt($new_phone, 'magic_mirror_decrypt_sms', PhoneCrypto::VERSIN);
        //清除用户的验证码缓存
        //清除手机号单日次数限制
        $day_result = Identifyingcode::clearIdentifyingCodeCache(Identifyingcode::PHONE_DAY_LIMIE_KEY,$new_phone);
        //清除手机号每小时次数限制
        $hour_result = Identifyingcode::clearIdentifyingCodeCache(Identifyingcode::PHONE_HOUR_LIMIE_KEY,$new_phone);

        if ($day_result || $hour_result) {
            AdmOperateLog::addLog($uid,AdmOperateLog::$operate_type['clear_identifying_code'],$admin_id,$admin_name);
            return $this->response()->success('缓存清除成功成功!')->refresh();
        } else {
            return $this->response()->error('暂不需要清除缓存!');
        }

    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function form($request)
    {
        $this->text('new_phone', '手机号')->required()->help('输入手机号');

    }
}